<template>
  <div class="register">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="username" name="username" label="用户名" placeholder="请输入用户名" left-icon="user-o"
          :rules="[{ required: true, message: '请填写用户名' }]" clearable />
        <van-field v-model="password" type="password" name="password" label="密码"  left-icon="shield-o" placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"  clearable/>
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit" color="#4A90E2">
          注册
        </van-button>
      </div>
    </van-form>
    <div style="display: flex;justify-content: center;">
      <router-link to="/login" class="link">已有账号？点击登录</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref} from 'vue';
import { showToast } from 'vant'
import { register } from '@/apis/auth'

const username = ref('');
const password = ref('');
const onSubmit = async (values) => {
  const {code,msg=''} =  await register({
    ...values,
  });
  if (code !== 200) {
    showToast(msg);
    return;
  }
  if (code === 200) {
    showToast('注册成功');
    return;
  }

};

</script>

<style lang="less" scoped>
.register {
  margin-top: 48px;
}
.link {
  color: #4A90E2;
}
</style>
